package xj.graph2d.app.prop;

import static xj.graph2d.app.resources.Resources.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Rectangle2D;
import java.text.DecimalFormat;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import xj.graph2d.Document;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

public class CanvasPropertyListener 
  extends DrawAction 
  implements ActionListener {

  class CanvasOptionPanel extends DialogPanel {

    public CanvasOptionPanel(String name) {
      super(name);

      JLabel canvasWidthLabel = new JLabel(getResourceString("CanvasWidth.text") + " ");
      JPanel canvasWidthPanel = new JPanel();
      canvasWidthPanel.setLayout(new BoxLayout(canvasWidthPanel, BoxLayout.X_AXIS));
      canvasWidthField = new JTextField();
      canvasWidthUnit = new JLabel("  " + getUnitString(unit) + "  ");
      canvasWidthPanel.add(canvasWidthField);
      canvasWidthPanel.add(canvasWidthUnit);
      canvasWidthDefault = new JButton(getResourceString("Default.text"));
      canvasWidthDefault.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  canvasWidthField.setText(pointsToUnitValue(Document.DEFAULT_CANVAS_WIDTH));
	}
      });

      JLabel canvasHeightLabel = new JLabel(getResourceString("CanvasHeight.text") + " ");
      JPanel canvasHeightPanel = new JPanel();
      canvasHeightPanel.setLayout(new BoxLayout(canvasHeightPanel, BoxLayout.X_AXIS));
      canvasHeightField = new JTextField();
      canvasHeightUnit = new JLabel("  " + getUnitString(unit) + "  ");
      canvasHeightPanel.add(canvasHeightField);
      canvasHeightPanel.add(canvasHeightUnit);
      canvasHeightDefault = new JButton(getResourceString("Default.text"));
      canvasHeightDefault.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  canvasHeightField.setText(pointsToUnitValue(Document.DEFAULT_CANVAS_HEIGHT));
	}
      });

      JLabel marginWidthLabel = new JLabel(getResourceString("MarginWidth.text") + " ");
      JPanel marginWidthPanel = new JPanel();
      marginWidthPanel.setLayout(new BoxLayout(marginWidthPanel, BoxLayout.X_AXIS));
      marginWidthField = new JTextField();
      marginWidthUnit = new JLabel("  " + getUnitString(unit) + "  ");
      marginWidthPanel.add(marginWidthField);
      marginWidthPanel.add(marginWidthUnit);
      marginWidthDefault = new JButton(getResourceString("Default.text"));
      marginWidthDefault.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  marginWidthField.setText(pointsToUnitValue(Document.DEFAULT_MARGIN));
	}
      });

      JLabel marginHeightLabel = new JLabel(getResourceString("MarginHeight.text") + " ");
      JPanel marginHeightPanel = new JPanel();
      marginHeightPanel.setLayout(new BoxLayout(marginHeightPanel, BoxLayout.X_AXIS));
      marginHeightField = new JTextField();
      marginHeightUnit = new JLabel("  " + getUnitString(unit) + "  ");
      marginHeightPanel.add(marginHeightField);
      marginHeightPanel.add(marginHeightUnit);
      marginHeightDefault = new JButton(getResourceString("Default.text"));
      marginHeightDefault.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  marginHeightField.setText(pointsToUnitValue(Document.DEFAULT_MARGIN));
	}
      });

      // unit selection panel
      JLabel unitLabel = new JLabel(getResourceString("Units.text") + " ");
      JPanel unitPanel = new JPanel();
      unitPanel.setLayout(new BoxLayout(unitPanel, BoxLayout.X_AXIS));
      unitChoice = new JComboBox();
      unitChoice.addItem(unitString[0]);
      unitChoice.addItem(unitString[1]);
      unitChoice.addItem(unitString[2]);
      unitChoice.addItemListener(new ItemListener() {
	public void itemStateChanged(ItemEvent event) {
	  if (event.getStateChange() == ItemEvent.SELECTED) {
	    Object item = event.getItem();
	    if (unitString[0].equals(item)) {
	      setUnit(Document.UNIT_PIXEL);
	    } else if (unitString[1].equals(item)) {
	      setUnit(Document.UNIT_INCH);
	    } else if (unitString[2].equals(item)) {
	      setUnit(Document.UNIT_CM);
	    }
	  }
	}
      });
      unitPanel.add(unitChoice);
      unitPanel.add(Box.createHorizontalStrut(60));
      // unitPanel.add(Box.createHorizontalGlue());
      unitDefault = new JButton(getResourceString("Default.text"));
      unitDefault.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  unitChoice.setSelectedIndex(0);
	  setUnit(Document.UNIT_PIXEL);
	}
      });

      addRow(canvasWidthLabel, canvasWidthPanel, canvasWidthDefault);
      addRow(canvasHeightLabel, canvasHeightPanel, canvasHeightDefault);
      addRow(marginWidthLabel, marginWidthPanel, marginWidthDefault);
      addRow(marginHeightLabel, marginHeightPanel, marginHeightDefault);
      addRow(unitLabel, unitPanel, unitDefault);

      setPreferredSize(new Dimension(370, 200));
      setOption();
    }

    @Override
    public void setOption() {
      SimpleDrawDocument doc = main.getDoc();
      SimpleDrawView view = main.getView();
      unit = Document.UNIT_PIXEL;
      Rectangle2D r = view.getVirtualCanvasRectangle();
      canvasWidth = (int) r.getWidth();
      canvasHeight = (int) r.getHeight();
      marginWidth = (int) view.getMarginWidth();
      marginHeight = (int) view.getMarginHeight();
      setInputFieldValues();
      setUnit(doc.getUnit());
      unitChoice.setSelectedItem(Document.getUnitString(unit));
    }

    @Override
    public void applyOption() {
      SimpleDrawDocument doc = main.getDoc();
      SimpleDrawView view = main.getView();
      getInputFieldValues();
      main.setCanvasDimension(canvasWidth, canvasHeight);
      view.setMarginWidth(marginWidth);
      view.setMarginHeight(marginHeight);
      doc.setUnit(unit);
      doc.setMarginWidth(marginWidth);
      doc.setMarginHeight(marginHeight);

      // Point2D p = view.getViewportLocation();
      // view.setViewportLocation((float) p.getX(), (float) p.getY());
    }

    protected String pointsToUnitValue(int p) {
      if (unit == Document.UNIT_PIXEL) {
	return Integer.toString(p);
      } else {
	float c = 1f;
	if (unit == Document.UNIT_INCH) {
	  c = Document.POINTS_PER_INCH;
	} else if (unit == Document.UNIT_CM) {
	  c = Document.POINTS_PER_CM;
	}
	// return Float.toString(p / c);
	return f.format(p / c);
      }
    }

    protected int unitValueToPoints(String v) {
      if (unit == Document.UNIT_PIXEL) {
	try {
	  return Integer.parseInt(v);
	} catch (NumberFormatException e) {
	}
      } else {
	float c = 1f;
	if (unit == Document.UNIT_INCH) {
	  c = Document.POINTS_PER_INCH;
	} else if (unit == Document.UNIT_CM) {
	  c = Document.POINTS_PER_CM;
	}
	try {
	  return (int) (Float.parseFloat(v) * c);
	} catch (NumberFormatException e) {
	}
      }
      return 0;
    }

    /**
     * Set the values to each text field based on the corresponding values
     * in the class. Perform conversions when necessary
     */
    protected void setInputFieldValues() {
      if (unit == Document.UNIT_PIXEL) {
	canvasWidthField.setText(Integer.toString(canvasWidth));
	canvasHeightField.setText(Integer.toString(canvasHeight));
	marginWidthField.setText(Integer.toString(marginWidth));
	marginHeightField.setText(Integer.toString(marginHeight));
      } else {
	float c = 1f;
	if (unit == Document.UNIT_INCH) {
	  c = Document.POINTS_PER_INCH;
	} else if (unit == Document.UNIT_CM) {
	  c = Document.POINTS_PER_CM;
	}
	canvasWidthField.setText(f.format(canvasWidth / c));
	canvasHeightField.setText(f.format(canvasHeight / c));
	marginWidthField.setText(f.format(marginWidth / c));
	marginHeightField.setText(f.format(marginHeight / c));
      }
    }

    /**
     * Get the values from each text field and set to the corresponding
     * values in the class. Perform conversions when necessary
     */
    protected void getInputFieldValues() {
      canvasWidth = Document.DEFAULT_CANVAS_WIDTH;
      canvasHeight = Document.DEFAULT_CANVAS_HEIGHT;
      marginWidth = Document.DEFAULT_MARGIN;
      marginHeight = Document.DEFAULT_MARGIN;
      if (unit == Document.UNIT_PIXEL) {
	try {
	  canvasWidth = Integer.parseInt(canvasWidthField.getText());
	} catch (NumberFormatException e) {
	}
	try {
	  canvasHeight = Integer.parseInt(canvasHeightField.getText());
	} catch (NumberFormatException e) {
	}
	try {
	  marginWidth = Integer.parseInt(marginWidthField.getText());
	} catch (NumberFormatException e) {
	}
	try {
	  marginHeight = Integer.parseInt(marginHeightField.getText());
	} catch (NumberFormatException e) {
	}
      } else {
	float c = 1f;
	if (unit == Document.UNIT_INCH) {
	  c = Document.POINTS_PER_INCH;
	} else if (unit == Document.UNIT_CM) {
	  c = Document.POINTS_PER_CM;
	}
	try {
	  canvasWidth = (int) (Float.parseFloat(canvasWidthField.getText()) * c);
	} catch (NumberFormatException e) {
	}
	try {
	  canvasHeight = (int) (Float.parseFloat(canvasHeightField.getText()) * c);
	} catch (NumberFormatException e) {
	}
	try {
	  marginWidth = (int) (Float.parseFloat(marginWidthField.getText()) * c);
	} catch (NumberFormatException e) {
	}
	try {
	  marginHeight = (int) (Float.parseFloat(marginHeightField.getText()) * c);
	} catch (NumberFormatException e) {
	}
      }
    }

    protected void setUnit(int u) {
      if (u != unit) {
	unit = u;
	setInputFieldValues();
	canvasWidthUnit.setText("  " + Document.getUnitString(unit) + "  ");
	canvasHeightUnit.setText("  " + Document.getUnitString(unit) + "  ");
	marginWidthUnit.setText("  " + Document.getUnitString(unit) + "  ");
	marginHeightUnit.setText("  " + Document.getUnitString(unit) + "  ");
      }
    }

    protected JTextField canvasWidthField;

    protected JTextField canvasHeightField;

    protected JTextField marginWidthField;

    protected JTextField marginHeightField;

    protected JLabel canvasWidthUnit;

    protected JLabel canvasHeightUnit;

    protected JLabel marginWidthUnit;

    protected JLabel marginHeightUnit;

    protected JButton canvasWidthDefault;

    protected JButton canvasHeightDefault;

    protected JButton marginWidthDefault;

    protected JButton marginHeightDefault;

    protected JComboBox unitChoice;

    protected JButton unitDefault;

    protected int unit = Document.UNIT_PIXEL;

    protected int canvasWidth;

    protected int canvasHeight;

    protected int marginWidth;

    protected int marginHeight;

  }

  static protected DecimalFormat f = new DecimalFormat("####0.000");

  static protected String[] unitString;  

  String getUnitString(int unit) { 
    switch (unit) {
    case Document.UNIT_INCH:
      return unitString[1];
    case Document.UNIT_CM:
      return unitString[2];
    case Document.UNIT_PIXEL:
    default:
      return unitString[0];
    }
  }

  protected TitledDialogPanel canvasOptionDialog = null;
  protected CanvasOptionPanel canvasOptionPanel = null;
  protected boolean showTitlePane = false;

  public void actionPerformed(ActionEvent e) {
    if (canvasOptionDialog == null) {
      unitString = new String[3];
      unitString[0] = getResourceString("Unit.pixel.text"); 
      unitString[1] = getResourceString("Unit.inch.text"); 
      unitString[2] = getResourceString("Unit.cm.text"); 

      //String canvasPanelTitle = getResourceString("Canvas.title");
      String canvasDialogTitle = getResourceString("CanvasPropertyDialog.title");
      String canvasHeader = getResourceString("CanvasPropertyDialog.header");
      String[] canvasText = new String[] { 
	getResourceString("CanvasPropertyDialog.text1"),
	getResourceString("CanvasPropertyDialog.text2"),	
      };
      Icon icon = DrawApp.getIcon("PrefWizIcon");    

      DialogPanel[] options = { 
	canvasOptionPanel = new CanvasOptionPanel(null), //canvasPanelTitle),
      };

      canvasOptionDialog = new TitledDialogPanel(main.getJFrame(), 
						 JOptionPane.OK_CANCEL_OPTION, 
						 null, options);
      canvasOptionDialog.setTitle(getResourceString("CanvasPropertyDialog.title"));
      if (showTitlePane) { 
	canvasOptionDialog.setTitlePaneText(canvasHeader, canvasText, icon);
      }
    }
    canvasOptionDialog.setOptions();
    canvasOptionDialog.showOptionDialog();
  }

}
